package com.my.blog.website.service.impl;

import com.my.blog.website.dao.RelationshipVoMapper;
import com.my.blog.website.modal.Vo.RelationshipVoKey;
import com.my.blog.website.service.IRelationshipService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by BlueT on 2017/3/18.
 */
@Service
public class RelationshipServiceImpl implements IRelationshipService {
    private static final Logger LOGGER = LoggerFactory.getLogger(RelationshipServiceImpl.class);

    @Resource
    private RelationshipVoMapper relationshipVoMapper;

    @Override
    public void deleteById(Integer cid, Integer mid) {
        /*RelationshipVoExample relationshipVoExample = new RelationshipVoExample();
        RelationshipVoExample.Criteria criteria = relationshipVoExample.createCriteria();*/
        Example example = new Example(RelationshipVoKey.class);
        Example.Criteria criteria = example.createCriteria();
        if (cid != null) {
            criteria.andEqualTo("cid",cid);
        }
        if (mid != null) {
            criteria.andEqualTo("mid",mid);
        }
        //relationshipVoMapper.deleteByExample(relationshipVoExample);
        relationshipVoMapper.deleteByExample(example);
    }

    @Override
    public List<RelationshipVoKey> getRelationshipById(Integer cid, Integer mid) {
        /*RelationshipVoExample relationshipVoExample = new RelationshipVoExample();
        RelationshipVoExample.Criteria criteria = relationshipVoExample.createCriteria();*/
        Example example = new Example(RelationshipVoKey.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("delFlag",0);
        if (cid != null) {
            //criteria.andCidEqualTo(cid);
            criteria.andEqualTo("cid",cid);
        }
        if (mid != null) {
            //criteria.andMidEqualTo(mid);
            criteria.andEqualTo("mid",mid);
        }
        return relationshipVoMapper.selectByExample(example);
    }

    @Override
    public void insertVo(RelationshipVoKey relationshipVoKey) {
        relationshipVoMapper.insert(relationshipVoKey);
    }

    @Override
    public Long countById(Integer cid, Integer mid) {
        LOGGER.debug("Enter countById method:cid={},mid={}",cid,mid);
        /*RelationshipVoExample relationshipVoExample = new RelationshipVoExample();
        RelationshipVoExample.Criteria criteria = relationshipVoExample.createCriteria();
        if (cid != null) {
            criteria.andCidEqualTo(cid);
        }
        if (mid != null) {
            criteria.andMidEqualTo(mid);
        }
        long num = relationshipVoMapper.countByExample(relationshipVoExample);*/
        Example example = new Example(RelationshipVoKey.class);
        Example.Criteria criteria = example.createCriteria();
        if (cid != null) {
            criteria.andEqualTo("cid",cid);
        }
        if (mid != null) {
            criteria.andEqualTo("mid",mid);
        }
        long num = relationshipVoMapper.selectCountByExample(example);
        LOGGER.debug("Exit countById method return num={}",num);
        return num;
    }

    @Override
    public Integer updateBySelective(RelationshipVoKey relationshipVoKey){
        Example example = new Example(RelationshipVoKey.class);
        Example.Criteria criteria = example.createCriteria();
        if(null != relationshipVoKey.getCid()){
            criteria.andEqualTo("cid",relationshipVoKey.getCid());
        }
        if(null != relationshipVoKey.getMid()){
            criteria.andEqualTo("mid",relationshipVoKey.getMid());
        }
        return relationshipVoMapper.updateByExampleSelective(relationshipVoKey,example);
    }
}
